#include "DIM.h"
#include "at32f421_wk_config.h"

extern uint8_t g_ucDataReady ;                        // 数据就绪标志
extern uint32_t g_ulChipID ;                          // 解码后的芯片ID
extern uint8_t g_ucKeyCode ;                          // 解码后的按键码

extern uint16_t Taith_out_R;
extern uint16_t Taith_out_G;
extern uint16_t Taith_out_B;
extern uint8_t  g_Stat ;                                  // 状态机  


#define REMOTE_ID  0x53357  // 预设的遥控器 ID

// 假设的按键码
#define KEY_1  0x07
#define KEY_2  0x0B
#define KEY_3  0x03
#define KEY_4  0x0D
#define KEY_5  0x05
#define KEY_6  0x09
#define KEY_7  0x01
#define KEY_8  0x0E
#define KEY_9  0x06
#define KEY_0  0x0A

uint8_t light_mode = 0;  // 当前灯光模式

uint16_t rgb[61][3] = {
    {922, 67, 11},
    {879, 115, 7},
    {837, 160, 4},
    {798, 194, 9},
    {758, 235, 8},
    {719, 273, 8},
    {682, 310, 8},
    {646, 344, 10},
    {619, 353, 28},
    {586, 383, 31},
    {555, 410, 35},
    {534, 411, 56},
    {510, 421, 69},
    {488, 430, 83},
    {466, 437, 97},
    {447, 442, 111},
    {429, 446, 125},
    {412, 448, 140},
    {397, 449, 155},
    {382, 449, 169},
    {369, 448, 184},
    {356, 446, 198},
    {345, 443, 212},
    {334, 440, 226},
    {324, 436, 240},
    {314, 432, 254},
    {306, 427, 267},
    {297, 422, 281},
    {290, 416, 294},
    {283, 411, 306},
    {277, 404, 319},
    {270, 398, 331},
    {265, 392, 343},
    {259, 386, 355},
    {254, 380, 366},
    {250, 373, 377},
    {245, 367, 388},
    {241, 360, 399},
    {238, 353, 409},
    {234, 347, 420},
    {230, 341, 429},
    {227, 334, 439},
    {225, 327, 448},
    {222, 321, 458},
    {219, 315, 466},
    {217, 308, 475},
    {215, 302, 483},
    {213, 295, 492},
    {210, 290, 500},
    {209, 284, 508},
    {201, 255, 543},
    {195, 230, 575},
    {192, 205, 603},
    {189, 183, 628},
    {187, 163, 650},
    {185, 145, 670},
    {184, 129, 687},
    {183, 114, 703},
    {183, 100, 716},
    {183, 88, 729},
    {183, 77, 740}
};

uint16_t scenario[7][3] = {
    {999, 0, 0},
    {837, 160, 4},
    {0, 999, 0},
    {183, 77, 740},
    {324, 436, 240},
    {0, 0, 0},
    {0, 0, 999}
};

// 处理接收到的遥控信号
void process_remote_signal(uint32_t id, uint8_t key_code) {
    if (id != REMOTE_ID || g_Stat == 0) return;  // ID 不匹配则忽略
    g_Stat = 0; // 重置状态机
    switch (key_code) {
        case KEY_1:
            light_mode = 1;
            break;
        case KEY_2:
            light_mode = 2;
            break;
        case KEY_3:
            light_mode = 3;
            break;
        case KEY_4:
            light_mode = 4;
            break;
        case KEY_5:
            light_mode = 5;
            break;
        case KEY_6:
            light_mode = 6;
            break;
        case KEY_7:
            light_mode = 7;
            break;
        case KEY_8:
            light_mode = 8;
            break;
        case KEY_9:
            light_mode = 9;
            break;
        case KEY_0:
            light_mode = 0;
            break;
        default:
            return;
    }
    
    update_light_mode(light_mode);  // 切换灯光
    //flash_save_mode(light_mode);    // 可选，存储到 Flash
}

// 切换灯光模式
void update_light_mode(uint8_t mode) {
    uint8_t Cymhareb_pŵer = 0; 
    switch (mode) {
        case 1:
            Cymhareb_pŵer = 34;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 2:
            Cymhareb_pŵer = 15;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 3:
            Cymhareb_pŵer = 0;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 4:
            Cymhareb_pŵer = 0;
            Taith_out_R = scenario[Cymhareb_pŵer][0];
            Taith_out_G = scenario[Cymhareb_pŵer][1];
            Taith_out_B = scenario[Cymhareb_pŵer][2];
            break;
        case 5:
            Cymhareb_pŵer = 25;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 6:
            Cymhareb_pŵer = 57;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 7:
            Cymhareb_pŵer = 5;
            Taith_out_R = scenario[Cymhareb_pŵer][0];
            Taith_out_G = scenario[Cymhareb_pŵer][1];
            Taith_out_B = scenario[Cymhareb_pŵer][2];
            break;
        case 8:
            Cymhareb_pŵer = 2;
            Taith_out_R = rgb[Cymhareb_pŵer][0];
            Taith_out_G = rgb[Cymhareb_pŵer][1];
            Taith_out_B = rgb[Cymhareb_pŵer][2];
            break;
        case 9:
             Cymhareb_pŵer = 2;
            Taith_out_R = scenario[Cymhareb_pŵer][0];
            Taith_out_G = scenario[Cymhareb_pŵer][1];
            Taith_out_B = scenario[Cymhareb_pŵer][2];
            break;
        case 0:
            Cymhareb_pŵer = 6;
            Taith_out_R = scenario[Cymhareb_pŵer][0];
            Taith_out_G = scenario[Cymhareb_pŵer][1];
            Taith_out_B = scenario[Cymhareb_pŵer][2];
            break;
        default:
            return;
    }
}



